import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/Login";
import Home from "../views/Home";
import ForgetPassword from "../views/userCenter/ForgetPassword";
import PersonalCenter from "../views/userCenter/PersonalCenter";
import PersonalPage from "../views/userPage/PersonalPage";
import PersonalSetting from "../views/userCenter/PersonalSetting";
import PersonalWorks from "../views/userCenter/PersonalWorks";
import ChangePassword from "../views/userCenter/ChangePassword";
import MyLikes from "../views/userCenter/MyLikes";
import Demands from "../views/demands/Demands";
import NewWork from "../views/works/NewWork";
import HotWork from "../views/works/HotWork";
import DemandsRelease from "../views/demands/DemandsRelease";
import MyDemands from "../views/userCenter/MyDemands";
import DemandDetails from "../views/demands/DemandDetails";
import DemandUserPage from "../views/userPage/DemandUserPage";
import Authors from "../views/author/Authors";
import SearchPage from "../views/search/SearchPage";
import ConfirmOrder from "../views/orders/ConfirmOrder";
import MyOrders from "../views/userCenter/MyOrders";
import CommentOrder from "../views/orders/CommentOrder";
import ReplyComment from "../views/userCenter/ReplyComment";
import DemandCategory from "../views/demands/DemandCategory";
import NewWorkCategory from "../views/works/NewWorkCategory";
import HotWorkCategory from "../views/works/HotWorkCategory";
import UploadWork from "../views/works/UploadWork";
import AuthorPage from "../views/userPage/AuthorPage";
import PersonalDemands from "../views/userPage/PersonalDemands";
import HerWorks from "../views/userPage/HerWorks";
import WorkDetail from "../views/works/WorkDetail";

Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        meta:{
            authLogin:true
        }
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path:'/forgetPassword',
        name:'ForgetPassword',
        component:ForgetPassword
    },
    {
        path:'/personalCenter',
        name:'PersonalCenter',
        component:PersonalCenter
    },
    {
        path:'/personalPage',
        name:'PersonalPage',
        component:PersonalPage
    },
    {
        path:'/personalWorks',
        name:'PersonalWorks',
        component:PersonalWorks
    },
    {
        path:'/personalSetting',
        name:'PersonalSetting',
        component:PersonalSetting
    },
    {
        path:'/changePassword',
        name:'ChangePassword',
        component:ChangePassword
    },
    {
        path:'/myLikes',
        name:'MyLikes',
        component:MyLikes
    },
    {
        path: '/demands',
        name:'Demands',
        component:Demands
    },
    {
        path:'/newWork',
        name:'NewWork',
        component:NewWork
    },
    {
        path:'/hotWork',
        name:'HotWork',
        component:HotWork
    },
    {
        path:'/demandsRelease',
        name:'DemandsRelease',
        component:DemandsRelease
    },
    {
        path:'/myDemands',
        name:'MyDemands',
        component:MyDemands
    },
    {
        path:'/demandDetails',
        name:'DemandDetails',
        component:DemandDetails
    },
    {
        path:'/demandUserPage',
        name:'DemandUserPage',
        component:DemandUserPage
    },
    {
        path:'/authors',
        name:'Authors',
        component:Authors
    },
    {
        path:'/searchPage',
        name:'SearchPage',
        component:SearchPage
    },
    {
        path:'/confirmOrder',
        name:'ConfirmOrder',
        component:ConfirmOrder
    },
    {
        path:'/myOrders',
        name:'MyOrders',
        component:MyOrders
    },
    {
        path:'/commentOrder',
        name:'CommentOrder',
        component:CommentOrder
    },
    {
        path:'/replyComment',
        name:'ReplyComment',
        component:ReplyComment
    },
    {
        path: '/demandCategory',
        name: 'DemandCategory',
        component: DemandCategory
    },
    {
        path:'/newWorkCategory',
        name: 'NewWorkCategory',
        component: NewWorkCategory
    },
    {
        path:'/hotWorkCategory',
        name: 'HotWorkCategory',
        component: HotWorkCategory
    },
    {
        path:'/uploadWork',
        name:'UploadWork',
        component:UploadWork
    },
    {
        path:'/authorPage',
        name:'AuthorPage',
        component:AuthorPage
    },
    {
        path:'/personalDemands',
        name:'PersonalDemands',
        component:PersonalDemands
    },
    {
        path:'/herWorks',
        name:'HerWorks',
        component:HerWorks
    },
    {
        path:'/workDetail',
        name:'WorkDetail',
        component:WorkDetail
    }

];

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
};
const router = new VueRouter({
    routes,
    mode: 'history',
    scrollBehavior (to, from, savedPosition) {
        return {x: 0, y: 0}
    },
});


router.beforeEach((to,from,next)=>{
    if (to.meta.authLogin){
        if (!localStorage.token && localStorage.username == null){
            router.replace({
                name:'Login'
            })
        }else {
            next()
        }
    }else {
        next()
    }
});

export default router
